<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="角色" prop="RoleID">
                <el-select v-model="ruleForm.RoleID" placeholder="请选择角色">
                    <el-option v-for="(item, index) in roles" :key="index"
                     :label="item.RoleName" :value="item.RoleID"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="姓名" prop="UserName">
                <el-input v-model="ruleForm.UserName"></el-input>
            </el-form-item>

            <el-form-item label="性别" prop="Sex">
                <el-radio-group v-model="ruleForm.Sex">
                    <el-radio label="男"></el-radio>
                    <el-radio label="女"></el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="出生日期" required>
                <el-col :span="11">
                    <el-form-item prop="Birthday">
                        <el-date-picker 
                        placeholder="选择日期" 
                        value-format="yyyy-MM-dd"
                        v-model="ruleForm.Birthday"
                            style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-form-item>

            <el-form-item label="手机号码" prop="Phone">
                <el-input v-model="ruleForm.Phone"></el-input>
            </el-form-item>
            <el-form-item label="账户" prop="LoginName">
                <el-input v-model="ruleForm.LoginName"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="LoginPwd">
                <el-input v-model="ruleForm.LoginPwd"></el-input>
            </el-form-item>

            <el-form-item label="状态" prop="Status">
                <el-switch v-model="ruleForm.Status"></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: 'userCreate',
    data() {
        return {
            //角色列表
            roles:[],
            //表单数据
            ruleForm: {
                UserID: 0,
                UserName: "张得帅",
                Sex: "女",
                Birthday: new Date("2008-08-08"),
                ImgID: 3,
                RoleID: 1,
                Phone: "13145678912",
                LoginName: "test1",
                LoginPwd: "123",
                Status: true
            },
            //表单验证
            rules: {
                UserName: [
                    { required: true, message: '请输入用户信息', trigger: 'blur' },
                    { min: 2, max: 12, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                RoleID: [
                    { required: true, message: '请选择角色', trigger: 'change' }
                ],
                Birthday: [
                    { required: true, message: '请选择日期', trigger: 'change' }
                ],
                Sex: [
                    { required: true, message: '请选择性别', trigger: 'change' }
                ],
                Phone: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度在必须为11位', trigger: 'blur' }
                ],
                LoginName: [
                    { required: true, message: '请输入账户', trigger: 'blur' }
                ],
                LoginPwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
            }
        }
    },

    props: {
        dialogVisible: Boolean
    },

    methods: {
        //提交
        submitForm(formName) {
            //先验证表单数据
            this.$refs[formName].validate((valid) => {
                if (valid) {

                    //添加数据
                    this.axios({
                        method: 'post',
                        url: 'users',
                        data: this.ruleForm
                    }).then(res => {

                        //判断是否操作成功
                        if (res.code == 1) {
                            this.$message({
                                message: res.msg,
                                type: 'success'
                            })

                            //关闭弹窗
                            this.$emit('update:dialogVisible', false);
                            //刷新父组件的列表数据
                            this.$emit('refresh');

                        } else {
                            this.$message({
                                message: res.msg,
                                type: 'error'
                            })
                        }

                    }).catch(err => {
                        this.$message({
                            message: err,
                            type: 'error'
                        })
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
    created() {
        this.axios({
            method:'get',
            url:'roles'
        }).then(res=>{
            this.roles=res.data;
        }).catch(err=>{})
    },

}
</script>

<style scoped></style>